package com.geekbg.zookeeper.leader;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import static com.geekbg.zookeeper.leader.Constants.SESSION_TIMEOUT;
import static com.geekbg.zookeeper.leader.Constants.ZOOKEEPER_STRING;

/**
 * @author pizhihui
 * @date 2024-06-04 22:41
 */
public class Master1Runner {


    public static void main(String[] args) throws IOException {


        try {
            ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_STRING, SESSION_TIMEOUT, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    System.out.println("wachedEvent: " + watchedEvent);

                }
            });

            //ArrayList<ACL> acls = Ids.CREATOR_ALL_ACL;
            TimeUnit.SECONDS.sleep(20);

                byte[] data = zooKeeper.getData("/tmp", new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("处理时间:" + watchedEvent);
                    }
                }, null);
                System.out.println("获取到数据: " + new String(data));




//            import org.apache.zookeeper.AddWatchMode;
//            zooKeeper.addWatch("/tmp", new Watcher() {
//                @Override
//                public void process(WatchedEvent event) {
//                    System.out.println("监听到/tmp 事件:"+ event);
//                }
//            }, AddWatchMode.PERSISTENT);

            //zooKeeper.create("/tmp/data", "data".getBytes(), acls, CreateMode.EPHEMERAL);
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.in.read();


    }

}
